package com.shf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shf.domain.ResponseResult;
import com.shf.domain.entity.Article;
import com.shf.domain.entity.Collect;

import com.shf.domain.vo.CollectVo;
import com.shf.domain.vo.PageVo;
import com.shf.service.CollectService;
import com.shf.mapper.CollectMapper;
import com.shf.util.BeanCopyUtils;
import com.shf.util.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 叶枫
* @description 针对表【sg_collect】的数据库操作Service实现
* @createDate 2024-05-13 15:46:47
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect>
    implements CollectService{

    @Resource
    private CollectMapper collectMapper;
    @Override
    public ResponseResult getUserCollect(String articleTitle, Integer currentPage, Integer pageSize) {

        Long userId = SecurityUtils.getUserId();
        if (userId != null) {

            List<Article> userCollects = collectMapper.getUserCollects(userId);
            int total = userCollects.size();
            List<Article> collect = userCollects.stream().skip((currentPage - 1) * pageSize)
                    .limit(pageSize)
                    .collect(Collectors.toList());

            List<CollectVo> collectVos = BeanCopyUtils.copyBeanList(collect, CollectVo.class);
            PageVo pageVo = new PageVo(collectVos, Integer.valueOf(total).longValue());

            return ResponseResult.okResult(pageVo);


        }
        return null;
    }
}




